<template>
  <el-select
    placeholder="请选择"
    v-model="value"
    @change="selectedCompany"
    :value-key="valueKey[0]"
    filterable
  >
    <el-option
      v-for="item in companyList"
      :key="item.id"
      :label="item[valueKey[1]]"
      :value="dataMode ? item : item.id"
    />
  </el-select>
</template>

<script>
import { companyList } from '@const/api'
export default {
  name: 'SelectCompany',
  data () {
    return {
      value: '',
      companyList: []
    }
  },
  props: {
    // 如果是true，则返回选中的整个对象，否则返回id
    dataMode: {
      type: Boolean,
      required: false,
      default: false
    },
    // 列表显示的label和value
    valueKey: {
      type: Array,
      required: false,
      default: () => {
        return ['id', 'fullName']
      }
    }
  },
  mounted () {
    this.findCompany()
  },
  methods: {
    findCompany () {
      this.$axios.$get(companyList).then(res => {
        this.companyList = res.payload || []
      })
    },
    selectedCompany (data) {
      this.$emit('selectedCompany', data)
    }
  }
}
</script>
<style scoped>
.copyright {
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;
  text-align: center;
  line-height: 50px;
  height: 50px;
  flex: 0 0 auto;
}
</style>
